<html data-dpr="4" style="font-size: 37.5px;">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>骡窝窝问答,解决旅行问题,基于旅游的问答分享平台 - 骡窝窝</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,minimal-ui,viewport-fit=cover">
    <meta name="format-detection" content="telephone=no,address=no">
    <meta name="referrer" content="always" />
    <meta name="apple-mobile-web-app-capable" content="no" />
    <meta name="apple-mobile-web-app-status-bar-style" content="no" />

    <link href="/css/mobile+css+head-mobile+css+ui-mobile+css+head_rem^Z1w^1535539097.css" rel="stylesheet" type="text/css">
    <link href="/css/mobile+css+head_rem-mobile+css+ask+new2017+ask-detail^ZlY^1537351708.css" rel="stylesheet" type="text/css">

    <script src="/js/plugins/jquery/jquery.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/common.js"></script>
    <script>
        var params;
        var currentPage = 1;
        var totalPage;
        var answerArr = [];
        var user;

        // 获取问题下所有回答
        function getAnswers() {
            // 请求数据
            $.get("/answers/" + params.id, {currentPage: currentPage, pageSize: 5}, function (data) {
                $.merge(answerArr, data.list);
                var json = {
                    list: answerArr
                };

                // 渲染数据
                $("#a-detail").renderValues(json, {
                    // 拼接回答id
                    getHref: function (item, value) {
                        var href = $(item).data("href");
                        $(item).attr('href', href + value);
                    },
                    getUser: function (item, value) {
                        var href = $(item).data("href");
                        $(item).attr('href', href + value);
                    },
                    // 回显金牌回答
                    getGolden: function (item, value) {
                        if (value) {
                            $(item).addClass("a-detail a-goldmedal")
                        } else {
                            $(item).addClass("a-detail")
                        }
                    },
                    // 顶相关
                    like: function (item, value) {
                        $(item).attr('onclick', "addLike("+value+",this)");
                        isLike(item, value)
                    },
                    // 收藏相关
                    fav: function (item, value) {
                        $(item).attr('onclick', "addFav("+value+",this)");
                        isFav(item, value)
                    }
                });
                totalPage = data.lastPage;
                currentPage = currentPage + 1;
            });
        }

        // 回显收藏
        function isFav(item, aid) {
            if (user){
                $.ajax({
                    url: "/users/"+user.id+"/answers/"+aid+"/isfav",
                    async: false, // key
                    success: function (data) {
                        if (data){
                            $(item).addClass("on");
                        }
                    }
                })
            }
        }

        // 收藏
        function addFav(aid, item) {
            if(!user){
                $(document).dialog({
                    type : 'confirm',
                    closeBtnShow: true,
                    content: '登陆后才可以收藏, 现在登录?',
                    onClickConfirmBtn: function(){
                        location.href = "/login.html?authorId=666";
                    }
                });
            }else{
                toggleFav(aid, item)
            }
        }

        // 切换收藏图标
        function toggleFav(aid, item) {
            if ($(item).hasClass("on")){
                $.ajax({
                    url: "users/"+user.id+"/answers/"+aid+"/delfav",
                    data: {uid: user.id, aid: aid},
                    type: "DELETE",
                    success: function (data) {
                        if(data.result){
                            $(item).removeClass("on");
                            $(item).children().html(data.result.favs)
                        }
                    }
                })
            } else {
                $.ajax({
                    url: "users/"+user.id+"/answers/"+aid+"/addfav",
                    data: {uid: user.id, aid: aid},
                    type: "PUT",
                    success: function (data) {
                        if(data.result){
                            $(item).addClass("on");
                            $(item).children().html(data.result.favs)
                        }
                    }
                })
            }
        }

        // 回显顶
        function isLike(item, aid) {
            if (user){
                $.ajax({
                    url: "/users/"+user.id+"/answers/"+aid+"/islike",
                    async: false, // key
                    success: function (data) {
                        if (data){
                            $(item).addClass("on");
                        }
                    }
                })
            }

        }

        // 顶
        function addLike(aid, item) {
            if(!user){
                $(document).dialog({
                    type : 'confirm',
                    closeBtnShow: true,
                    content: '登陆后才可以顶, 现在登录?',
                    onClickConfirmBtn: function(){
                        location.href = "/login.html?authorId=666";
                    }
                });
            }else{
                toggleLike(aid, item)
            }
        }

        // 切换顶图标
        function toggleLike(aid, item) {
            if ($(item).hasClass("on")){
                $.ajax({
                    url: "users/"+user.id+"/answers/"+aid+"/dellike",
                    data: {uid: user.id, aid: aid},
                    type: "DELETE",
                    success: function (data) {
                        if(data.result){
                            $(item).removeClass("on");
                            $(item).children().html(data.result.likes)
                        }
                    }
                })
            } else {
                $.ajax({
                    url: "users/"+user.id+"/answers/"+aid+"/addlike",
                    data: {uid: user.id, aid: aid},
                    type: "PUT",
                    success: function (data) {
                        if(data.result){
                            $(item).addClass("on");
                            $(item).children().html(data.result.likes);
                        }
                    }
                })
            }
        }

        $(function () {
            user = JSON.parse(sessionStorage.getItem("user"));
            var followCount;

            $("#_j_add_answer").click(function () {
                if(!user){
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '登陆后才可以回答, 现在登录?',
                        onClickConfirmBtn: function(){
                            location.href = "/login.html?authorId=666";
                        }
                    });
                }else{
                    location.href = "/mine/addanswer.html?id=" + params.id;
                }
            })

            // 获取问题id
            params = getParams()
            if (params.id){
                // 获取问题
                $.get("/questions/" + params.id, function (data) {
                    $(".q-detail").renderValues(data,{
                        getUser: function (item, value) {
                            var href = $(item).data("href");
                            $(item).attr('href', href + value);
                        }
                    });

                    // 关注
                    followCount = data.followCount;
                    var a_follow = $("#_j_care_question");
                    //a_follow.attr("onclick", "addFollow("+params.id+", this)");
                    isFollow(a_follow, params.id)
                });
            }

            //默认查询第一页的数据
            getAnswers();

            // 点击更多加载更多
            $("#moreanswer").click(function () {
                if (totalPage > currentPage){
                    getAnswers();
                }else{
                    $(document).dialog({
                        type : 'notice',
                        infoText: '没有更多啦',
                        autoClose: 1000,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                }
            });

            // 回显关注
            function isFollow(item, qid){
                if (user){
                    $.ajax({
                        url: "/users/"+user.id+"/questoins/"+qid+"/isfollow",
                        async: false, // key
                        success: function (data) {
                            if (data){
                                console.log(1);
                                $(item).addClass("on");
                                $("#follow").html("已关注("+followCount+")");
                            } else {
                                $("#follow").html("关注问题("+followCount+")");
                            }
                        }
                    })
                } else {
                    //console.log(1);
                    $("#follow").html("关注问题("+followCount+")");
                }
            }

            // 点击关注
            $("#_j_care_question").click(function () {
                if (!user) {
                    $(document).dialog({
                        type : 'confirm',
                        closeBtnShow: true,
                        content: '登陆后才可以关注, 现在登录?',
                        onClickConfirmBtn: function(){
                            location.href = "/login.html?authorId=666";
                        }
                    });
                } else {
                   toggleFollow(params.id, this);
                }
            });

            // 关注/取消关注
            function toggleFollow(qid, item){
                if ($(item).hasClass("on")){
                    $.ajax({
                        url: "users/"+user.id+"/questoins/"+qid+"/delfollow",
                        data: {uid: user.id, qid: qid},
                        type: "DELETE",
                        success: function (data) {
                            if(data.success){
                                $(item).removeClass("on");
                                $("#follow").html("关注问题("+(followCount - 1)+")");
                                followCount -= 1;
                            }
                        }
                    })
                } else {
                    $.ajax({
                        url: "users/"+user.id+"/questoins/"+qid+"/addfollow",
                        data: {uid: user.id, qid: qid},
                        type: "PUT",
                        success: function (data) {
                            if(data.success){
                                $(item).addClass("on");
                                $("#follow").html("已关注("+(followCount + 1)+")");
                                followCount += 1;
                            }
                        }
                    })
                }
            }
        });

    </script>
</head>
<body style="font-size: 36px;">
<header class="head2home">
        <a class="logo" href="javascript:void(window.history.length &gt; 1 ? window.history.back() : document.location.href= '/index.html')"
                         id="_j_top_history_back"></a>
    <div class="rBtn">
        <a href="/index.html" class="home">官网首页</a>
        <a href="#" class="sch"></a>
    </div>
    </div>
</header>

<input type="hidden" name="currentPage" id="currentPage" value="1">

<div class="container no-padding">
    <!--问题-->
    <div class="q-detail">
        <h3 class="title" render-html="title"></h3>
        <p class="desc" render-html="content"></p>
        <div class="stat">
            <span class="nums"><b render-html="viewCount"></b>浏览 · <b render-html="answerCount"></b>回答 · <b render-html="commentCount"></b>评论</span>

        </div>
        <div class="meta">
            <span class="location">全球</span>

            <a data-href="/mine/userProfiles.html?id=" render-key="list.user.id" render-fun="getUser"><span class="user"><img render-src="user.headImgUrl"><span render-html="user.nickName"></span> 问于<span class="time" render-html="createTime"></span></span></a>
        </div>
    </div>
    <!--问题下所有回答-->
    <div id="a-detail">
        <div render-loop="list">
            <div data-class="a-detail" render-key="list.golden" render-fun="getGolden">
                <!--跳转用户信息页面-->
                <span class="da"><a hrdata-href="/mine/userProfiles.html?id=" render-key="list.user.id" render-fun="getUser"><img render-src="list.user.headImgUrl"></a></span>
                <div class="hd">
                    <a class="name" data-href="/mine/userProfiles.html?id=" render-key="list.user.id" render-fun="getUser" render-html="list.user.nickName"></a>
                    <div class="user-lv lv-s3">Lv25</div>
                    <span class="time" render-html="list.createTime"></span>
                </div>
                    <!--部分回答正文-->
                <div class="bd">
                    <div class="expandable"  style="max-height: 9.533rem;" >
                        <p render-html="list.content">
                        </p>
                        <div class="expand-more">
                            <p><a data-href="/answerdetail.html?id=" render-key="list.id" render-fun="getHref">继续阅读</a></p>
                        </div>
                    </div>
                </div>
                <div class="ft">
                    <!--点赞-->
                    <a class="btn-ding _j_vote" render-key="list.id" render-fun="like"><b render-html="list.likes"></b></a>
                    <!--评论-->
                    <a data-href="/answerdetail.html?id=" render-key="list.id" render-fun="getHref" class="btn-comment _j_comment" >评论 <b render-html="list.commentCount"></b></a>
                    <a class="btn-collect _j_collect" render-key="list.id" render-fun="fav">收藏 <b render-html="list.favs"></b></a>
                </div>
            </div>
            <div class="space"></div>
        </div>
    </div>
</div>

<div class="fixed-bot default-pos">
    <a class="item" id="moreanswer">更多答案</a>
</div>
<div class="fixed-bot default-pos">
    <a class="item " id="_j_care_question" render-key="id" render-fun="follow"><i class="icon-follow"></i><span id="follow"></span></a>
    <a class="item" id="_j_add_answer"><i class="icon-addanswer"></i>添加答案</a>
</div>

</body>
</html>